const {fail} = require("@/utils/helper");
import {setCookie} from "@/utils/common";
import axios from "@/utils/axios";

export default {
    namespaced: true,
    state: {
        userinfo: {},
        userEditPop: false,
    },
    getters: {

    },
    mutations: {

    },
    actions: {
        async login({ commit,dispatch}, payload) {
            const {mobile, password} = payload;
            const loginRes = await axios.post('/login', {mobile, password})
            if (!loginRes['success']) return fail(loginRes['message']);
            setCookie('authorization', loginRes.data['authorization'], 24 * 30);
            return await dispatch('userinfo');
        },
        async userinfo({ state, commit }) {
            const userinfoRes = await axios.get('/userinfo');
            if (!userinfoRes['success']) return fail(userinfoRes['message']);
            state.userinfo = userinfoRes.data;
            return userinfoRes;
        }
    }
}
